package com.npf.imgserver.config;

import com.npf.imgserver.common.constant.Constants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //(静态资源)图片url映射
        //windows 环境
        //registry.addResourceHandler("/images/**").addResourceLocations(Constants.IMG_MAPPING_PATH_WINDOWS);
        //Linux 环境
        //registry.addResourceHandler("/images/**").addResourceLocations(Constants.IMG_MAPPING_PATH_LINUX);
        //Mac os 环境
        registry.addResourceHandler("/images/**")
                .addResourceLocations(Constants.IMG_MAPPING_PATH_MAC);

    }

    /**
     * 浏览器输出 UTF_8
     *
     * @return
     */
    @Bean
    public HttpMessageConverter<String> responseBodyStringConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        return converter;
    }

    /**
     * 修改StringHttpMessageConverter默认配置
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(responseBodyStringConverter());
    }

}
